package main

import (
	"github.com/astaxie/beego"

	"cvevulner/common"
	"cvevulner/cve-ddd/infrastructure/obsimpl"
	cve_timed_task "cvevulner/cve-timed-task"
	"cvevulner/models"
	_ "cvevulner/models"
	"cvevulner/routers"
	_ "cvevulner/routers"
	"cvevulner/task"
	"cvevulner/taskhandler"
)

func init() {
	// Initialize global variables
	common.InitGlobal()
	// Initialization log
	common.LogInit()
}

func main() {
	// init db
	dbOk := models.Initdb()
	if !dbOk {
		println("error: Database initialization failed")
		return
	}

	if err := obsimpl.Init(); err != nil {
		println("error: obs initialization failed")
		return
	}

	taskhandler.InitReleaseDate()

	// Initialize a scheduled task
	taskOk := task.InitTask()
	if !taskOk {
		println("error: Timing task initialization failed, the program ends")
		task.StopTask()
		return
	}

	err := cve_timed_task.StartCveTimedTask()
	if err != nil {
		println("StartCveTimedTask, error: ", err)
		return
	}
	// single run
	task.StartTask()
	defer task.StopTask()
	if beego.BConfig.RunMode == "dev" {
		beego.BConfig.WebConfig.DirectoryIndex = true
		beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	}

	routers.Init()

	beego.Run()
}
